
Page({
  data:{
    avatarUrl: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    nickName:"微信用户",
    isLogin:false
  },
  onShareAppMessage(res){
    return {
      title:'易眼电影',
      path:'pages/tabBar/movie/movie'
    }
  },
  chooseavatar(event){
    const {avatarUrl} =event.detail
    this.setData({
      avatarUrl
    })
    console.log(event)
    console.log(avatarUrl)
    // var avatarUrl_base64 = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(this.data.avatarUrl, 'base64')
    
     //将编码后的图片发送到服务器进行存储
     wx.request({
      url: 'http://localhost:8080/user/url',
      method: 'POST',
      header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token':wx.getStorageSync('token')
      },
      data: {
          avatarUrl: avatarUrl 
      },
      success(res) {
          console.log(res)
      }
  })
  },
  getNickName(event){
    var username = event.detail.value
    wx.request({
        url: 'http://localhost:8080/user/name',
        method: 'POST',
        header: {
            'content-type': 'application/x-www-form-urlencoded',
            'token':wx.getStorageSync('token')
        },
        data: {
            username: username, 
        },
        success(res) {
            console.log(res)
            this.setData({
              nickName:res.data.data.userName
            })
        }
    })
  },
  WXlogin(){
    wx.login({
      success: (res) => {
        console.log(res)
        var code=res.code
        wx.request({
          url: 'http://localhost:8080/user/login',
          method:'POST',
          header:{
            'content-type': 'application/x-www-form-urlencoded'
          },
          data:{
            "code":code
          },
          success:(res)=>{
          wx.setStorageSync('token', res.data.data.token)
          console.log(res)
          this.setData({
            isLogin:true,
            avatarUrl:res.data.data.userAvatarUrl,
            nickName:res.data.data.userName
          })
          }
        })
      },
    })
  }
})